<?php
namespace Api\Model;

use Think\Model;

class PUserModel extends ApiBaseModel
{
    /**
     * url前半部分
     */
    private $BASE_URL = "https://api.miaodiyun.com/20150822/";

    /**
     * url中的accountSid。如果接口验证级别是主账户则传网站“个人中心”页面的“账户ID”，
     */

    //$ACCOUNT_SID = "50c2a7af880b45b992df3f3c42exxxxx"; // 主账户
    //$AUTH_TOKEN = "0c6b801cccd34cc1b22ab8aeaeexxxxx";

  //  private $ACCOUNT_SID = "7d829ae07e8d440780bfe765358cb07e";
  //  private $AUTH_TOKEN = "afed8b02671946ef819b3840702e341f";

    private $ACCOUNT_SID = "daa473f46faf4bb79ad1551b89ad57f1";
    private $AUTH_TOKEN = "d24011fdce1347a9b0d69a9f2539edad";

    /**
     * 请求的内容类型，application/x-www-form-urlencoded
     */
    private $CONTENT_TYPE = "application/x-www-form-urlencoded";


    
    /**
     * 期望服务器响应的内容类型，可以是application/json或application/xml
     */
    private $ACCEPT = "application/json";

    private $funAndOperate = "industrySMS/sendSMS";

    //网易云信
    private $appKey = "2f7fdd945992452ccc39c59de60019f5";
    private $appSecret = "071c85c8b3a4";
    private $sendUrl = "https://api.netease.im/sms/sendcode.action";
    private $tempUrl = "https://api.netease.im/sms/sendtemplate.action";
    private $verifyUrl = "https://api.netease.im/sms/verifycode.action";


    public function send($phone)
    {
        $body['templateid'] = 3039250;
        $body['mobile'] = $phone;
       // $body['mobiles'] = json_encode(array($phone));
       // $body['params'] = json_encode(array($code));
        $result = $this->postSend($this->sendUrl, $body);
        return json_decode($result, true);
    }

    public function reset($phone, $password)
    {
        $body['mobiles'] = json_encode(array($phone));
        $body['templateid'] = 3048011;
        $body['params'] = json_encode(array($password));
        $result = $this->postSend($this->tempUrl, $body);
        return json_decode($result, true);
    }
    public function verify($phone, $code)
    {
        $body['mobile'] = $phone;
        $body['code'] = $code;

        $result = $this->postSend($this->verifyUrl, $body);
        return json_decode($result,true);
    }
    private function checkSum($nonce, $time)
    {
        return sha1($this->appSecret.$nonce.$time);
    }

    private function postSend($url, $body)
    {
        $nonce = rand(1, 100000);
        $time = time();
        $checkSum = $this->checkSum($nonce, $time);
        // 构造请求数据
        $headers = array("Content-Type:application/x-www-form-urlencoded;charset=utf-8");
        $headers[] = "AppKey:".$this->appKey;
        $headers[] = "Nonce:".$nonce;
        $headers[] = "CurTime:".$time;
        $headers[] = "CheckSum:".$checkSum;

        // 要求post请求的消息体为&拼接的字符串，所以做下面转换
        $fields_string = "";
        foreach ($body as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');

        // 提交请求
        $con = curl_init();
        curl_setopt($con, CURLOPT_URL, $url);
        curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($con, CURLOPT_HEADER, 0);
        curl_setopt($con, CURLOPT_POST, 1);
        curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($con, CURLOPT_POSTFIELDS, $fields_string);
        $result = curl_exec($con);
        curl_close($con);

        return $result;
    }

    public function sendSMS($content, $phone){
        $body = $this->createBasicAuthData();
        // 在基本认证参数的基础上添加短信内容和发送目标号码的参数
       $body['smsContent'] = "【浦发】验证码：".$content."，打死都不要告诉别人哦！";
        //$body['smsContent'] = "【秒嘀科技】您的验证码是".$content."，30分钟内有效。";
        $body['to'] = $phone;

        // 提交请求
        $result = $this->post($this->funAndOperate, $body);
        $result = json_decode($result, true);
        return $result;
    }

    public function sendPassSMS($content, $phone){
        $body = $this->createBasicAuthData();
        // 在基本认证参数的基础上添加短信内容和发送目标号码的参数
        $body['smsContent'] = "【浦发】重置后的密码是：".$content."，请妥善保管！";
        //$body['smsContent'] = "【秒嘀科技】您的验证码是".$content."，30分钟内有效。";
        $body['to'] = $phone;

        // 提交请求
        $result = $this->post($this->funAndOperate, $body);
        $result = json_decode($result, true);
        return $result;
    }

    private function createBasicAuthData()
    {
        $timestamp = date("YmdHis");
        // 签名
        $sig = md5($this->ACCOUNT_SID . $this->AUTH_TOKEN . $timestamp);
        return array("accountSid" => $this->ACCOUNT_SID, "timestamp" => $timestamp, "sig" => $sig, "respDataType"=> "JSON");
    }

    /**
     * 创建请求头
     * @param body
     * @return
     */
    function createHeaders()
    {
        $headers = array('Content-type: ' . $this->CONTENT_TYPE, 'Accept: ' . $this->ACCEPT);
        return $headers;
    }


    /**
     * 创建url
     *
     * @param funAndOperate
     *            请求的功能和操作
     * @return
     */
    function createUrl($funAndOperate)
    {
        // 时间戳
        date_default_timezone_set("Asia/Shanghai");
        $timestamp = date("YmdHis");

        return $this->BASE_URL . $funAndOperate;
    }
    function post($funAndOperate, $body)
    {
        // 构造请求数据
        $url = $this->createUrl($funAndOperate);
        $headers = $this->createHeaders();

        // 要求post请求的消息体为&拼接的字符串，所以做下面转换
        $fields_string = "";
        foreach ($body as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');

        // 提交请求
        $con = curl_init();
        curl_setopt($con, CURLOPT_URL, $url);
        curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($con, CURLOPT_HEADER, 0);
        curl_setopt($con, CURLOPT_POST, 1);
        curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($con, CURLOPT_POSTFIELDS, $fields_string);
        $result = curl_exec($con);
        curl_close($con);

        return $result;
    }

}
